//
//  QMyCouponsVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/8.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QMyCouponsVC.h"
#import "QMyCouponsModel.h"
#import "QCouponHeadView.h"
#import "QCouponCell.h"
@interface QMyCouponsVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tbView;

@end

@implementation QMyCouponsVC{

    NSMutableArray *_dataArr;
    
    QCouponCell *_couponCell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠券";
    _dataArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self data];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(15, 74, 180, 20);
    label.textColor = [QColorUtility defaultBackColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentLeft;
    label.text = @"1张优惠券即将过期";
    [self.view addSubview:label];
    
    WS(wSelf);
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset(104);
        make.left.equalTo(wSelf.view.mas_left).offset(10);
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(-10);
        make.right.equalTo(wSelf.view.mas_right).offset(-10);
    }];
    [self.tbView registerNib:[UINib nibWithNibName:@"QCouponCell" bundle:nil] forCellReuseIdentifier:@"QCouponCell"];
    _couponCell = [_tbView dequeueReusableCellWithIdentifier:@"QCouponCell"];
    
}

//假数据
- (void)data{
    NSArray *arr_1 = @[@"使用规则:",@" A--->标题A--->标题A--->标题A--->标题A--->标题A--->标题A--->标题",@" A--->标题"];
    NSArray *arr_2 = @[@"使用规则:",@" B--->标题B--->标题B--->标题B--->标题B--->标题B--->标题B--->标题"];
    NSArray *arr_3 = @[@"使用规则:",@" C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题",@" C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题"];
    NSArray *arr_4 = @[@"使用规则:",@" D--->标题",@" D--->标题"];
    NSArray *arr_5 = @[@"使用规则:",@" B--->标题B--->标题B--->标题B--->标题B--->标题B--->标题B--->标题"];
    NSArray *arr_6 = @[@"使用规则:",@" C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题",@" C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题C--->标题"];
    NSArray *arr_7 = @[@"使用规则:",@" D--->标题",@" D--->标题"];
    NSArray *datas = @[arr_1,arr_2,arr_3,arr_4,arr_5,arr_6,arr_7];

    for(int i=0; i<datas.count; ++i){
        QMyCouponsModel *model = [[QMyCouponsModel alloc] init];
        model.dataArr = datas[i];
        [_dataArr addObject:model];
    }
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    QMyCouponsModel *model = _dataArr[section];
    if(model.isShow){
        return model.dataArr.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QCouponCell" forIndexPath:indexPath];
    QMyCouponsModel *model = _dataArr[indexPath.section];
    cell.titleLabel.text = model.dataArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 90;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 50;
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QMyCouponsModel *model = _dataArr[indexPath.section];
    _couponCell.dataString = model.dataArr[indexPath.row];
    return [_couponCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    QCouponHeadView *headView = [[[NSBundle mainBundle] loadNibNamed:@"QCouponHeadView" owner:self options:nil] lastObject];
    if(section %2 ==0){
        [headView.bgButton setBackgroundImage:[UIImage imageNamed:@"redcoupon_image"] forState:UIControlStateNormal];
    }else{
        [headView.bgButton setBackgroundImage:[UIImage imageNamed:@"blueCoupon_image"] forState:UIControlStateNormal];

    }
    return headView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth-20, 50)];
    view.backgroundColor =[UIColor groupTableViewBackgroundColor];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-20, 40)];
    [button setTitle:@"详细信息" forState:UIControlStateNormal];
    QMyCouponsModel *model = _dataArr[section];
    if(model.isShow){
        [button setImage:[UIImage imageNamed:@"show_top"] forState:UIControlStateNormal];
    }else{
        [button setImage:[UIImage imageNamed:@"show_bottom"] forState:UIControlStateNormal];
    }
    [button setBackgroundImage:[UIImage imageNamed:@"bottomCoupon_image"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.tag = section;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    cell.frame = CGRectMake(-kScreenWidth, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
//    [UIView animateWithDuration:.5 animations:^{
//        cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
//        
//    }];
//}

- (void)buttonAction:(UIButton *)button{

    QMyCouponsModel *model = _dataArr[button.tag];
    model.isShow = !model.isShow;
    [_tbView reloadSections:[NSIndexSet indexSetWithIndex:button.tag] withRowAnimation:UITableViewRowAnimationFade];
}


#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tbView.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tbView;
}


@end
